package com.transcargo.action;

import com.transcargo.errors.ErrorList;
import com.transcargo.db.db;
import com.transcargo.db.dbException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import java.sql.SQLException;

/**
 * Date: 06.05.2011
 * Time: 13:21:40
 */
public class AddMailAction extends AuthAction {
    private String recipient = "";
    private String priority = "";
    private String subject = "";
    private String text = "";
    private int id = 0;

    private void servletGetValues(HttpServletRequest request) throws ServletParametersException {
        recipient = request.getParameter("recipient");
        priority = request.getParameter("priority");
        subject = request.getParameter("subject");
        text = request.getParameter("text");

        if (recipient == null) recipient = "";
        if (priority == null) priority = "";
        if (subject == null) subject = "";
        if (text == null) text = "";
    }
    private void servletValidateValues() throws ServletValidationException {

        if (recipient.length() == 0 || Integer.parseInt(recipient) == 0) throw new ServletValidationException(ErrorList.ERROR_STR_INVALID_RECIPIENT);
        if (priority.length() == 0 || Integer.parseInt(priority) == 0) throw new ServletValidationException(ErrorList.ERROR_STR_PRIORITY_MISSING);
        if (subject.length() == 0) throw new ServletValidationException(ErrorList.ERROR_STR_SUBJECT_MISSING);
        if (subject.length() > 255) throw new ServletValidationException(ErrorList.ERROR_STR_SUBJECT_TOO_LONG);
        if (text.length() == 0) throw new ServletValidationException(ErrorList.ERROR_STR_BODY_MISSING);
        if (text.length() > 100*1024) throw new ServletValidationException(ErrorList.ERROR_STR_BODY_TOO_LONG);
    }

    private void servletExecuteAction() throws ServletActionException {
        db.PreStatement pre = null;
        try {
            pre = dbase.CreateStatement("CALL CreateMail(?, ?, ?, ?, ?)");
            pre.Insert(User_Id);
            pre.Insert(Integer.parseInt(recipient));
            pre.Insert(Integer.parseInt(priority));
            pre.Insert(subject);
            pre.Insert(text);
            dbase.executeUpdate(pre);
        } catch (SQLException e) {
			System.out.println("SQL exception: " + e.getMessage());
			throw new ServletActionException();
		} catch (dbException e) {
			System.out.println("Db exception: " + e.getMessage());
			throw new ServletActionException();
		}
    }
    public void processAction(HttpServletRequest request, HttpServletResponse response) throws ServletException {
		try {
            servletGetValues(request);
            servletValidateValues();
            servletExecuteAction();
            sendRedirect(response, "mail.jsp?i=1");
	    } catch (ServletParametersException e) {
            if (e.getMessage() != null) {
                sendError(response, new Exception(e.getMessage()));
            } else {
                sendRedirect(response, "mail.jsp");
            }
        }  catch (ServletValidationException e) {
            sendError(response, new Exception(e.getMessage()));
        }  catch (ServletActionException e) {
            sendError(response, new Exception("" + ErrorList.ERROR_STR_GENERIC + dbase.getError()));
        }
    }
}
